//
//  ViewController.m
//  WeSignSDKLibDemo
//
//  Created by apple on 16/7/13.
//  Copyright © 2016年 signit. All rights reserved.
//

#import "ViewController.h"
#import "WeSignAPI.h"
@interface ViewController ()
{
    WeSignAPI *api;
    NSString *userID;
    NSString *certID;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //用户登录
    api = [[WeSignAPI alloc]init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onLogin:(id)sender {
    userID = [api loginWithUser:@"wt@signit.cn" password:@"pwe-kGF-G3z-eQ9" withToken:@""];
    if (userID) {
          UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"用户登录成功，UserID为" message:userID delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else {
          UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"用户登录失败" message:nil     delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
         [alert show];
    }
   
}

- (IBAction)onhasCertSign:(id)sender {
    
    NSString *file = [[NSBundle mainBundle]pathForResource:@"企事业单位呈批件" ofType:@"pdf"];
    NSData *data = [NSData dataWithContentsOfFile:file];
    
    NSData *seal = UIImagePNGRepresentation([UIImage imageNamed:@"seal.png"]);
    //获取证书ID
    NSArray *certIDs= [api listCertificatesWithUserid:userID];
    //使用证书签名PDF文件
    NSDictionary *signInfo2 =    @{@"ulX":@0,
                                   @"ulY":@0,
                                   @"lrX":@100,
                                   @"lrY":@100,
                                   @"page":@1,
                                   @"pageZoom":@0.75,
                                   @"sealImgBytes":seal,
                                   @"pdfBytes":data,
                                   @"certId":certIDs[0]};
    NSData *signedData2 =  [api hasCertSignDocWithUserid:userID signInfo:signInfo2];
    [signedData2 writeToFile:[NSString stringWithFormat:@"%@/Documents/cert_signed.pdf", NSHomeDirectory()] atomically:YES];
    NSLog(@"签名后的文件地址：%@/Documents/cert_signed.pdf", NSHomeDirectory());

    
    
    if (signedData2) {
        UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"用户签名成功" message:@"文件存放地址请查看Xcode控制台输出" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else {
        UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"用户签名失败" message:nil     delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    }

}

- (IBAction)onOffice2PDF:(id)sender {
    
    NSString *file = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"docx"];
    NSURL *url = [NSURL fileURLWithPath:file];
    
    //Office 文档转PDF
    NSData *data = [api Office2PDFWithUserid:userID fileURL:url];
    [data writeToFile:[NSString stringWithFormat:@"%@/Documents/sample.pdf", NSHomeDirectory()] atomically:YES];
    NSLog(@"转换后文件地址：%@/Documents/sample.pdf", NSHomeDirectory());
    
    
    NSData *data2 = [api Office2PDFWithUserid:userID withData:[NSData dataWithContentsOfFile:file] withFileName:@"test.docx"];
    [data2 writeToFile:[NSString stringWithFormat:@"%@/Documents/sample2.pdf", NSHomeDirectory()] atomically:YES];
    NSLog(@"转换后文件地址：%@/Documents/sample2.pdf", NSHomeDirectory());
    
    if (data2) {
        UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"文档转换成功" message:@"文件存放地址请查看Xcode控制台输出" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else {
        UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"文档转换失败" message:nil     delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    }

}

- (IBAction)onnoCertSign:(id)sender {
    
    NSData *seal = UIImagePNGRepresentation([UIImage imageNamed:@"seal.png"]);
    NSString *file = [[NSBundle mainBundle]pathForResource:@"企事业单位呈批件" ofType:@"pdf"];
    NSData *data = [NSData dataWithContentsOfFile:file];

    //无证书签名PDF文件
    NSDictionary *signInfo =    @{@"ulX":@0,
                                  @"ulY":@0,
                                  @"lrX":@100,
                                  @"lrY":@100,
                                  @"page":@1,
                                  @"pageZoom":@0.75,
                                  @"sealImgBytes":seal,
                                  @"pdfBytes":data};
    NSData *signedData =  [api noCertSignDocWithUserid:userID signInfo:signInfo ];
    [signedData writeToFile:[NSString stringWithFormat:@"%@/Documents/nocert_signed.pdf", NSHomeDirectory()] atomically:YES];
    NSLog(@"签名后的文件地址：%@/Documents/nocert_signed.pdf", NSHomeDirectory());
    
    if (signedData) {
        UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"用户签名成功" message:@"文件存放地址请查看Xcode控制台输出" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    } else {
        UIAlertView *alert =  [[UIAlertView alloc]initWithTitle:@"用户签名成功" message:nil     delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
        [alert show];
    }

}

@end
